using Microsoft.AspNetCore.Mvc;
using UniversalAdminSystem.Application.Authentication.DTOs;
using UniversalAdminSystem.Application.Authentication.Interfaces;
using UniversalAdminSystem.Application.Common.Results;
using UniversalAdminSystem.Domian.UserManagement.ValueObj;

namespace UniversalAdminSystem.Api.Controllers;

[ApiController]
[Route("api/auth")]
public class AuthenticationController : ControllerBase
{
    private readonly ILoginAppService _loginAppService;
    private readonly IRegisterAppService _registerAppService;
    private readonly IJwtTokenService _jwtTokenService;

    public AuthenticationController(
        ILoginAppService loginAppService,
        IRegisterAppService registerAppService,
        IJwtTokenService jwtTokenService)
    {
        _loginAppService = loginAppService;
        _registerAppService = registerAppService;
        _jwtTokenService = jwtTokenService;
    }

    [HttpPost("login")]
    public async Task<IActionResult> Login([FromBody] LoginDto loginDto)
    {
        try
        {
            if (string.IsNullOrEmpty(loginDto.Account) || string.IsNullOrEmpty(loginDto.Password))
            {
                return BadRequest(Result<LoginResultDto>.Failure("账号和密码不能为空"));
            }

            var result = await _loginAppService.LoginAsync(loginDto);
            return Ok(Result<LoginResultDto>.Success(result));
        }
        catch (Exception ex)
        {
            return BadRequest(Result<LoginResultDto>.Failure(ex.Message));
        }
    }

    [HttpPost("register")]
    public async Task<IActionResult> Register([FromBody] RegisterDto registerDto)
    {
        try
        {
            if (string.IsNullOrEmpty(registerDto.Account) || 
                string.IsNullOrEmpty(registerDto.Password) || 
                string.IsNullOrEmpty(registerDto.Email))
            {
                return BadRequest(Result<RegisterResultDto>.Failure("账号、密码和邮箱不能为空"));
            }

            var result = await _registerAppService.RegisterAsync(registerDto);
            return Ok(Result<RegisterResultDto>.Success(result));
        }
        catch (Exception ex)
        {
            return BadRequest(Result<RegisterResultDto>.Failure(ex.Message));
        }
    }

    [HttpPost("refresh-token")]
    public IActionResult RefreshToken([FromBody] RefreshTokenDto refreshTokenDto)
    {
        try
        {
            if (string.IsNullOrEmpty(refreshTokenDto.Token))
            {
                return BadRequest(Result<RefreshTokenResultDto>.Failure("Token不能为空"));
            }

            var newToken = _jwtTokenService.RefreshToken(
                refreshTokenDto.Token, 
                out string userId, 
                out string roleId,
                out UserStatus status);

            var result = new RefreshTokenResultDto(newToken, userId, roleId);
            return Ok(Result<RefreshTokenResultDto>.Success(result));
        }
        catch (Exception ex)
        {
            return BadRequest(Result<RefreshTokenResultDto>.Failure(ex.Message));
        }
    }

    [HttpPost("logout")]
    public IActionResult Logout()
    {
        return Ok(Result.Success("登出成功"));
    }
} 